/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[欢迎加盟知启蒙，一起邂逅框架梦]
 * 
 * https://www.zhiqim.com/gitcan/zhiqim/zhiqim_account.htm
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.zhiqim.account;

import java.util.List;

import org.zhiqim.account.dbo.user.UserAccount;
import org.zhiqim.account.dbo.user.UserInfo;
import org.zhiqim.httpd.HttpSessionUser;
import org.zhiqim.httpd.context.ZmlContextConstants;
import org.zhiqim.kernel.annotation.AnAlias;
import org.zhiqim.kernel.util.Validates;

/**
 * 客户会话对象
 *
 * @version v1.0.0 @author zouzhigang 2018-4-10 新建与整理
 */
@AnAlias("ZacSessionUser")
public class ZacSessionUser extends HttpSessionUser implements ZmlContextConstants, ZacConstants
{ 
    private UserInfo user;
    private String userAccount;                //登录账号，手机号或邮箱
    private List<UserAccount> accountList;  //账号列表
    
    @Override
    public String getSessionName()
    {
        return userAccount;
    }
    
    public String getRootPath(String path)
    {
        return getContext().getRootPath(path);
    }
    
    /***************************************************************************************/
    //设置用户账号&信息
    /***************************************************************************************/
    
    public ZacSessionUser setUser(UserInfo user)
    {
        this.user = user;
        return this;
    }
    
    public ZacSessionUser setAccountList(List<UserAccount> accountList)
    {
        this.accountList = accountList;
        return this;
    }

    public ZacSessionUser setUserAccount(String userAccount)
    {
        this.userAccount = userAccount;
        return this;
    }
    
    /***************************************************************************************/
    //用户相关信息
    /***************************************************************************************/
    
    public UserInfo getUser()
    {
        return user;
    }

    public String getUserAccount()
    {
        return userAccount;
    }
    
    public String getUserName()
    {
        return Validates.isEmpty(user.getUserName())?"":user.getUserName();
    }
    
    public long getUserId()
    {
        return user.getUserId();
    }
    
    public String getUserNick()
    {
        return user.getUserNick();
    }
    
    public String getUserAvatar()
    {
        return Validates.isEmpty(user.getUserAvatar50())?getRootPath(_PATH_SERVICE_RES_AVATAR_):user.getUserAvatar50();
    }

    public String getUserAvatar100()
    {
        return Validates.isEmpty(user.getUserAvatar100())?getRootPath(_PATH_SERVICE_RES_AVATAR_):user.getUserAvatar100();
    }
    
    public String getUserAvatar150()
    {
        return Validates.isEmpty(user.getUserAvatar150())?getRootPath(_PATH_SERVICE_RES_AVATAR_):user.getUserAvatar150();
    }
    
    public String getUserEmail()
    {
        return user.getUserEmail();
    }
    
    public String getUserMobile()
    {
        return user.getUserMobile();
    }
    
    /***************************************************************************************/
    //用户账号相关信息
    /***************************************************************************************/
    
    public List<UserAccount> getAccountList()
    {
        return accountList;
    }
    
    public String getAccountMobile()
    {
        for (UserAccount account : accountList)
        {
            if (account.getAccountType() == ACCOUNT_TYPE_MOBILE)
                return account.getUserAccount();
        }
        
        return null;
    }
    
    public String getAccountEmail()
    {
        for (UserAccount account : accountList)
        {
            if (account.getAccountType() == ACCOUNT_TYPE_EMAIL)
                return account.getUserAccount();
        }
        
        return null;
    }
    
    public String getAccountQQ()
    {
        for (UserAccount account : accountList)
        {
            if (account.getAccountType() == ACCOUNT_TYPE_QQ)
                return account.getUserAccount();
        }
        
        return null;
    }
    
    public String getAccountWxop()
    {
        for (UserAccount account : accountList)
        {
            if (account.getAccountType() == ACCOUNT_TYPE_WXOP)
                return account.getUserAccount();
        }
        
        return null;
    }
    
    public String getAccountWxmp()
    {
        for (UserAccount account : accountList)
        {
            if (account.getAccountType() == ACCOUNT_TYPE_WXMP)
                return account.getUserAccount();
        }
        
        return null;
    }
}